/**
 *	bbx 
 */
define(function(require, exports, module) {
	"use strict";
	//每一个页面都要引入的工具类
	var CommonTools = require('CommonTools_Core');

	//页面跳转管理类
	var WindowTools = require('WindowTools_Core');
	var baseService = require('Base_Service');
	var trainService = require('Train_Service');
	var enumHelper = require('Common_Enum');
	
	var goodId = 0;//商品
	var price = 0;//单价

	//每一个页面都要引入的工具类
	// initready 要在所有变量初始化做完毕后
	CommonTools.initReady(initData);
	/**
	 * @description 初始化数据,结合initReady使用
	 * plus情况为plusready
	 * 其它情况为直接初始化
	 */
	var user;
	function initData() {
		//引入必备文件,下拉刷新依赖于mui与mustache
		CommonTools.importFile([
			'js/libs/mui.min.js'
		], function() {
			mui.init({
				statusBarBackground: '#f7f7f7'
			});
			user = baseService.getUser();
			WindowTools.preloadTemplate();
			initHomePage();
			initListeners();
		});
	}
	/**
	 * ui的初始化
	 */
	function initHomePage() {
		goodId = WindowTools.getExtraDataByKey("goodId");
		var drivingTitle = WindowTools.getExtraDataByKey("drivingTitle");
		var placeName = WindowTools.getExtraDataByKey("placeName");
		var drivingLicenseType = WindowTools.getExtraDataByKey("drivingLicenseType");
		var courseType = WindowTools.getExtraDataByKey("courseType");
		var timeSoltType = WindowTools.getExtraDataByKey("timeSoltType");
		price = WindowTools.getExtraDataByKey("price");	
		mui("#DrivingTitle")[0].innerHTML = drivingTitle;
		mui("#PlaceName")[0].innerHTML = placeName;
		mui("#DrivingLicenseType")[0].innerHTML = enumHelper.drivingLicenseTypeFormat(drivingLicenseType);
		mui("#CourseType")[0].innerHTML = enumHelper.courseTypeFormat(courseType);
		mui("#TimeSoltType")[0].innerHTML = enumHelper.timeSoltTypeFormat(timeSoltType);
		mui("#TotalFee")[0].innerHTML = price + "元";
	}

	/**
	 * 点击事件初始化
	 */
	function initListeners() {
//		var btnPay = document.getElementById('btnPay'); //提交按钮
		var btnHistory = document.getElementById('history'); //提交按钮

//		btnHistory.addEventListener("tap",function(){
//			WindowTools.openWinWithTemplate("","train_history.html");
//		});

		btnPay.addEventListener("tap", function() {
			var data = {
				"GoodID": goodId,
				"TotalHours": mui("#TotalHours")[0].value
			};
			trainService.add(data, function(data) {
				WindowTools.createWin(null, "train_add_success.html", {
					'mid': data.TrainID
				}, {});
			});
		});
		
		mui("#TotalHours")[0].addEventListener("change",function(){
			var totalHours = mui("#TotalHours")[0].value;
			totalHours = totalHours || 1;
			mui("#TotalFee")[0].innerHTML = totalHours * price + "元";
		});
	}
});